<?php
namespace Ucmaim\Controller\Admin;

class GoodsController extends CommonController{

    public function _initialize(){
        parent::_initialize();
    }
    public function Lists(){
        $do = I('get.do');

        switch($do) {
            case 'Verify':
                    $get = I('get.');
                    $id = $get['id'];
                    $status = $get['status'];
                    $result = D('goods')->updateGoodsStatus($id,$status);
                    $result ? $this->success('操作成功') : $this->error('操作失败');
                break;

            case 'Edit':
                if ( IS_POST ) {
                    if($_POST['goods_id'] <= 0 ) $this->error('非法操作');
                    $result = D('goods')->edit();
                    $result == 1 ? $this->success('修改成功') : $this->error('修改失败');
                } else {
                    $gid = (int)I('get.id');
                    if($gid <= 0) $this->error('操作错误');
                    $goods = D('goods')->getGoodsInfo($gid, false);

                    if(empty($goods)) $this->error('该商品不存在');
                    if(!empty($goods['goods_pic'] )) $goods['goods_pic'] = explode(",",$goods['goods_pic']);
                    $this->assign('goods', $goods);
                    $this->display('Goods');
                }
                break;

            case 'Search':
                $post  = I('post.');

                if( ! empty($post) ){
                    session('search', $post);
                }elseif( ! I('get.Page') ){
                    session('search', null);
                }
                $post = session('search') ? session('search') : [];

                //筛选开始
                $map  = [];
                //商品状态筛选
                if($post['status'] !='' ) {
                    $map['A.goods_status'] = (int)$post['status'];
                }

                //关键词筛选
                if( $post['keyword'] !='' ) {
                    $where['A.goods_title'] = $where['D.member_name'] = $where['D.member_mobile'] = array('LIKE', '%'. $post['keyword'] .'%');
                    $where["_logic"] = 'or';
                    $map['_complex'] = $where;
                }

                //时间筛选
                $date  = trim($post['date']);
                if($date){
                    $tmp = explode('~', $date);
                    $start_date = trim($tmp[0]);
                    $end_date   = trim($tmp[1]);
                    $start = strtotime($start_date);
                    $end   = strtotime($end_date) + 86400;  //end需要加一天，防止当天的数据搜索不出来
                    $map['_string'] = '(A.goods_time>'.$start.') and (A.goods_time<'.$end.')';
                }
                $this->assign('start',$tmp[0]);
                $this->assign('end',$tmp[1]);


                $page = (int)I('get.Page');
                $page = $page > 0 ? $page : 1;
                $vlist = D('goods')->getAll($page, $this->pageSize, $show_page,$map);
                $this->assign('show_page', $show_page);
                $this->assign('vlist', $vlist);
                $this->display('Index');
                break;

            default:
                $get = I('get.');
                $page = (int)$get['Page'];
                $page = $page > 0 ? $page : 1;
                $vlist = D('goods')->getAll($page, $this->pageSize, $show_page);

                $this->assign('show_page', $show_page);
                $this->assign('vlist', $vlist);
                $this->display('Index');
                break;
        }
    }
}